package com.ls.io.rpc.transport;

import com.ls.io.rpc.protobuf.RpcContent;
import com.ls.io.rpc.protobuf.RpcHeader;
import com.ls.io.rpc.protobuf.RpcPackage;
import com.ls.io.rpc.utils.SeriUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.IOException;

public class ServerChannelHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        RpcPackage rpcPackage = (RpcPackage)msg;

        RpcHeader header = rpcPackage.getHeader();
        RpcContent content = rpcPackage.getContent();

//        System.out.println("server getIfcName:" + content.getIfcName());
//        System.out.println("server getMethodName:" + content.getMethodName());
//        System.out.println("server getParameterTypes:" + content.getParameterTypes());
//        System.out.println("server getArgs:" + content.getArgs()[0]);

        System.out.println("recv requestId: " + header.getRequestId() + " content:" + content.getArgs()[0]);

        String ioThreadName = Thread.currentThread().getName();

//        ctx.executor().parent().next().execute(() -> {
//        ctx.executor().execute(() -> {
            try {
                String execThreadName = Thread.currentThread().getName();

                // 回复消息
                String respMsg = String.format("io thread: %s, exec thread: %s, args: %s", ioThreadName, execThreadName, content.getArgs()[0]);
                RpcContent respContent = new RpcContent();
                respContent.setResult(respMsg);

                RpcPackage respPck = new RpcPackage(header, respContent);

                ChannelFuture channelFuture = ctx.writeAndFlush(respPck);
//                channelFuture.sync();

                System.out.println("server response:" + content.getArgs()[0]);
            }catch (Exception e) {
                e.printStackTrace();
            }
//        });

    }

    /*@Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf)msg;

        if(buf.readableBytes() > 20){
            int flag = buf.readInt();
            long requestId = buf.readLong();
            long length = buf.readLong();


            if(buf.readableBytes() >= length){
                byte[] bytes = new byte[(int)length];
                buf.readBytes(bytes);
                ByteArrayInputStream biso = new ByteArrayInputStream(bytes);
                ObjectInputStream is = new ObjectInputStream(biso);
                RpcContent content = (RpcContent) is.readObject();

//                System.out.println("server getIfcName:" + content.getIfcName());
//                System.out.println("server getMethodName:" + content.getMethodName());
//                System.out.println("server getParameterTypes:" + content.getParameterTypes());
                System.out.println("server getArgs:" + content.getArgs()[0]);


                // 回复消息
                String respMsg = "yes, i have received msg : " + requestId + " " + content.getArgs()[0];

                RpcHeader header =  new RpcHeader();
                header.setFlag(flag);
                header.setRequestId(requestId);
                header.setLength(respMsg.getBytes().length);

                ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(20 + respMsg.getBytes().length);
                byteBuf.writeBytes(header.toArray());
                byteBuf.writeBytes(respMsg.getBytes());
                ctx.writeAndFlush(byteBuf);
            }

        }
    }*/

}
